package com.wk.code.wkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.code.wkserver.entity.AdminVerifyRecord;
import com.wk.code.wkserver.mapper.AdminVerifyRecordMapper;
import com.wk.code.wkserver.response.admin.AdminVerifyRecordResponse;
import com.wk.code.wkserver.service.IAdminVerifyRecordService;
import com.wk.code.wkserver.service.ISysUserService;
import com.wk.code.wkserver.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-08-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AdminVerifyRecordServiceImpl extends ServiceImpl<AdminVerifyRecordMapper, AdminVerifyRecord> implements IAdminVerifyRecordService {


    @Resource
    private ISysUserService sysUserService;

    @Override
    public void save(Integer dataId, Integer sysUserId, Integer verifyStatus, String remark, Integer type) {
        AdminVerifyRecord record = new AdminVerifyRecord();
        record.setDataId(dataId);
        record.setSysUserId(sysUserId);
        record.setVerifyStatus(verifyStatus);
        record.setRemark(remark);
        record.setType(type);
        save(record);
    }

    @Override
    public List<AdminVerifyRecordResponse> selectList(Integer dataId, Integer type) {
        List<AdminVerifyRecord> recordList = baseMapper.selectList(new LambdaQueryWrapper<AdminVerifyRecord>().eq(AdminVerifyRecord::getDataId, dataId).
                eq(AdminVerifyRecord::getType, type));
        if (!CollectionUtils.isEmpty(recordList)) {
            return recordList.stream().map(x -> {
                AdminVerifyRecordResponse response = new AdminVerifyRecordResponse();
                response.setCreateTime(DateUtils.localDateTime2Date(x.getCreateTime()));
                response.setId(x.getId());
                response.setRemark(x.getRemark());
                response.setVerifyerName(sysUserService.getById(x.getSysUserId()).getNickname());
                return response;
            }).collect(Collectors.toList());
        }
        return null;
    }
}
